using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Deloitte.Common;
using Deloitte.Data;

namespace Deloitte.Windows.Controls
{
    public partial class DataGridViewControl : UserControl
    {
        ExcelExportControl excelExportControl;
        DataViewRowFilterControl filterControl;
        DataViewSortControl sortControl;
        DistinctColumnValuesControl distinctColumnValuesControl;
        DataView dataView;
        public DataGridViewControl(DataTable table)
        {
            InitializeComponent();        
            CommonConstructorCode(table);
        }
        public DataGridViewControl(DataView view)
        {
            InitializeComponent();
            CommonConstructorCode(view);
        }
        public DataGridViewControl()
        {
            InitializeComponent();
            CommonConstructorCode(null);
        }
        private void CommonConstructorCode(object dataSource)
        {
            SetDataSource(dataSource);
            this.SetDataGridEvents();
            this.splitContainer1.Panel2.ControlAdded += new ControlEventHandler(panelOptions_ControlAdded);
            this.filterText.TextChanged += new EventHandler(filterText_TextChanged);
            this.controlsPanel.ControlAdded += new ControlEventHandler(controlsPanel_ControlAdded);
            this.SetQuickFilter();
        }

        private void filterText_TextChanged(object sender, EventArgs e)
        {
            string filter = string.Empty;            
            try
            {
                if (this.dataView.Table.Columns[this.columnsComboBox.Text].DataType == typeof(string))
                {
                    filter = "[" + this.columnsComboBox.Text + "] LIKE '%" + this.filterText.Text + "%'";
                    this.dataView.RowFilter = filter;
                    this.errorProvider1.Clear();
                }
            }
            catch { this.errorProvider1.SetError(this.dataGridView1, "Invalid Filter Statement: \r" + filter); }
        }

        void controlsPanel_ControlAdded(object sender, ControlEventArgs e)
        {
            e.Control.Size = new Size(this.controlsPanel.Width, this.controlsPanel.Height);
            e.Control.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
        }

        private void SetDataSource(object dataSource)
        {
            //todo: add support for a list, dataset, binding source
            if (dataSource is DataTable){ 
                this.dataGridView1.DataSource = ((DataTable)dataSource).DefaultView;
                this.dataView = ((DataTable)dataSource).DefaultView; 
            }
            else if (dataSource is DataView) {
                this.dataGridView1.DataSource = (DataView)dataSource;
                this.dataView = (DataView)dataSource;
            }                   
        }
        private void SetDataGridEvents()
        {
            try
            {
                dataGridView1.DataSourceChanged += new EventHandler(dataGridView1_DataSourceChanged);
                this.Resize += new EventHandler(DataGridViewControl_Resize);
            }
            catch { }
        }
        private void DataGridViewControl_Resize(object sender, EventArgs e)
        {
         //   if (this.ctlOptions != null) this.ctlOptions.Size = this.splitContainer1.Panel2.Size;
        }
        private void panelOptions_ControlAdded(object sender, ControlEventArgs e)
        {
            if (this.dataGridView1.Size.Height == this.splitContainer1.Panel1.Size.Height) this.dataGridView1.Size = new Size(this.splitContainer1.Size.Width, (int)(this.splitContainer1.Size.Height * .75));
        }
        
        private void dataGridView1_DataSourceChanged(object sender, EventArgs e) 
        {
            SetQuickFilter();
        }

        private void SetQuickFilter()
        {
            this.columnsComboBox.Items.Clear();
            for (int i = 0; i < this.dataGridView1.Columns.Count; i++) { this.columnsComboBox.Items.Add(this.dataGridView1.Columns[i].HeaderText); }
            this.filterText.Text = "";
        }

        private void excelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddExcelExportControl();
        }

        private void AddExcelExportControl()
        {
            excelExportControl = new ExcelExportControl(Utility.CreateDataTable(this.dataGridView1));
            controlsPanel.Controls.Add(this.excelExportControl);
            controlsPanel.Controls[this.excelExportControl.Name].BringToFront();
        }

        private void filterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddFilterControl();
        }
        private void AddFilterControl()
        {
            if(this.filterControl == null){this.filterControl = new DataViewRowFilterControl(this.dataView);}
            controlsPanel.Controls.Add(this.filterControl);
            controlsPanel.Controls[this.filterControl.Name].BringToFront();
        }

        private void sortToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.sortControl == null) { this.sortControl = new DataViewSortControl(this.dataView); }
            controlsPanel.Controls.Add(this.sortControl);
            controlsPanel.Controls[this.sortControl.Name].BringToFront();
        }

        private void distinctColumnValuesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.distinctColumnValuesControl = new DistinctColumnValuesControl(Utility.CreateDataTable(this.dataGridView1));
            controlsPanel.Controls.Add(this.distinctColumnValuesControl);
            controlsPanel.Controls[this.distinctColumnValuesControl.Name].BringToFront();
        }

        private void excelToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AddExcelExportControl();
        }

        private void csvToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SaveViewToCSV();
        }
     
        private void SaveViewToCSV()
        {
            this.saveFileDialog1.Title = @"Save CSV File";
            this.saveFileDialog1.Filter = @"CSV File (*.csv)|*.csv";
            if (this.saveFileDialog1.ShowDialog()== DialogResult.OK)
            {
                Utility.ExportToCSV(this.dataGridView1, System.IO.Path.GetDirectoryName(this.saveFileDialog1.FileName), System.IO.Path.GetFileName(this.saveFileDialog1.FileName));
            }
        }

        private void csvToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveViewToCSV();
        }

        private void xMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveViewToXML();
        }
        public void SaveViewToXML()
        {
            this.saveFileDialog1.Title = @"Save XML File";
            this.saveFileDialog1.Filter = @"XML File (*.xml)|*.xml";
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                DataTable table = Utility.CreateDataTable(this.dataGridView1);
                table.WriteXml(this.saveFileDialog1.FileName);
            }
        }

        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataTable2 table = new DataTable2(this.dataGridView1);
        }
    }
}
